/**
 * 
 */
package com.feng.dao;

import com.feng.model.AdminUser;
import com.feng.model.Bill;
import com.feng.util.PageResult;
import com.feng.util.Search;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 帐单DAO
 */
@Repository
public class BillDao extends BaseDao<Bill> {
	
	/**
	 * 分页查询代理商
	 * @return
	 */
	public PageResult<Bill> findByPage(Search search,AdminUser user)  throws Exception {
		PageResult<Bill> ret = new PageResult<Bill>();
		ret.setCurrentPage(search.getCurrentPage());
		ret.setPageSize(search.getPageSize());
		
		String hql = "from Bill u where 1=1 ";
		List<Object> params = new ArrayList<Object>();
		if(user!=null && user.getRole().getId().equals(3l)){
			hql += " and u.op = ?";
			params.add(user.getId());
		}
		if (!StringUtils.isEmpty(search.getNo()) ) {
			hql += " and u.insuranceNo like ? escape '/' ";
			params.add("%" + search.getNo() + "%");
		}
		if (!StringUtils.isEmpty(search.getCompany()) ) {
			hql += " and u.company like ? escape '/' ";
			params.add("%" + search.getCompany() + "%");
		}
		if (!StringUtils.isEmpty(search.getName()) ) {
			hql += " and u.name like ? escape '/' ";
			params.add("%" + search.getName() + "%");
		}
		if (!StringUtils.isEmpty(search.getLicensePlate()) ) {
			hql += " and u.licensePlate like ? escape '/' ";
			params.add("%" + search.getLicensePlate() + "%");
		}
		if (!StringUtils.isEmpty(search.getOrderBy()) ) {
			hql += " order by "+search.getOrderBy();
		}

		this.findPageList(hql, params,ret);
	    return ret;
	}


	public Bill findByNo(String insuranceNo){
		List<Bill> list = this.find("from Bill u where u.insuranceNo = ?",insuranceNo);
		Bill ret = null;
		if(list.size()>0){
			ret = list.get(0);
		}
		return ret;
	}

	public Bill findByName(String name){
		List<Bill> list = this.find("from Bill u where u.name = ?",name);
		Bill ret = null;
		if(list.size()>0){
			ret = list.get(0);
		}
		return ret;
	}
	public Bill findByPlate(String licensePlate){
		List<Bill> list = this.find("from Bill u where u.licensePlate = ?",licensePlate);
		Bill ret = null;
		if(list.size()>0){
			ret = list.get(0);
		}
		return ret;
	}


	public Bill findExactRec(Bill bill) {

		return null;
	}
}
